//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameRuntime
// *描述：框架初始化脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace HDMCode.UIFrame
{
    public class UIFrameRuntime : MonoBehaviour
    {
        private readonly string CANVAS = "Canvas";
        private readonly string UIDIALOG = "UIDialog";
        private readonly string UIFIXATION = "Fixation";
        private readonly string FULLSCREEN = "FullScreen";
        private readonly string INFORM = "Inform";
        private readonly string BACKWALL = "BackWall";

        //获取Canvas及弹窗，固定，通用层级
        public Canvas Canvas { get; private set; }
        
        /// <summary>
        /// 固定UI层级节点
        /// </summary>
        public GameObject Fixation { get; private set; }
        
        /// <summary>
        /// 通知UI层级节点
        /// </summary>
        public GameObject Inform { get; private set; }
        
        /// <summary>
        /// 全屏UI层级节点
        /// </summary>
        public GameObject FullScreen { get; private set; }
        
        /// <summary>
        /// 弹窗UI层级节点
        /// </summary>
        public GameObject Dialog { get; private set; }
        
        /// <summary>
        /// 当前背景墙节点
        /// </summary>
        public (Transform wall,Image image) BackWall { get;private set; }
        
        /// <summary>
        /// 当前缓存器
        /// </summary>
        public UIFrameStoreExpand FrameStore { get;internal set; }

        /// <summary>
        /// 初始化UI框架初始层级，层级初始化顺序有严格显示，不可随意修改
        /// </summary>
        private void Awake()
        {
            try
            {
                this.Canvas = GameObject.Find(CANVAS).GetComponent<Canvas>();
                FullScreen = Canvas.transform.Find(FULLSCREEN)?.gameObject ?? CreatUIChild(FULLSCREEN, new Vector4(0, 0, 1, 1));
                //Dialog = Canvas.transform.Find(UIDIALOG)?.gameObject ?? CreatUIChild(UIDIALOG, new Vector4(0.1f, 0.1f, 0.9f, 0.9f));
                Dialog = Canvas.transform.Find(UIDIALOG)?.gameObject ?? CreatUIChild(UIDIALOG, new Vector4(0f, 0f, 1f, 1f));
                //Inform = Canvas.transform.Find(INFORM)?.gameObject ?? CreatUIChild(INFORM, new Vector4(0.5f, 0.5f, 0.9f, 0.9f));
                Inform = Canvas.transform.Find(INFORM)?.gameObject ?? CreatUIChild(INFORM, new Vector4(0f, 0f, 1f, 1f));
                Fixation = Canvas.transform.Find(UIFIXATION)?.gameObject ?? CreatUIChild(UIFIXATION, new Vector4(0f, 0f, 1f, 1f));
                BackWall = BaffleWall(Canvas.transform, BACKWALL);
                DontDestroyOnLoad(Canvas.gameObject);
                FrameStore = new UIFrameStoreExpand();
            }
            catch (Exception error)
            {
                UIFrame.LogError("UI框架层级处理失败，请尝试检查【 Canvas 】！", error, LogSystem.UIModle);
            }
        }

        /// <summary>
        /// 创建默认UI层级
        /// </summary>
        /// <param name="childName"> 子对象名称 </param>
        /// <param name="vector"> 默认图层大小 </param>
        /// <returns></returns>
        private GameObject CreatUIChild(string childName, Vector4 vector)
        {
            GameObject newChild = new GameObject(childName);
            newChild.transform.SetParent(Canvas.transform);
            newChild.transform.localPosition = new Vector3(0,0,0);
            newChild.transform.localScale = new Vector3(1, 1, 1);
            newChild.transform.localRotation = new Quaternion(0,0,0,0);
            RectTransform rectTransform = newChild.AddComponent<CanvasRenderer>().gameObject.AddComponent<RectTransform>();
            rectTransform.anchorMin = new Vector2(vector.x, vector.y);
            rectTransform.anchorMax = new Vector2(vector.z, vector.w);
            rectTransform.offsetMin = rectTransform.offsetMax = new Vector2(0, 0);
            return newChild;
        }

        /// <summary>
        /// UI的遮挡面板
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="childName"></param>
        /// <returns></returns>
        private (Transform, Image) BaffleWall(Transform parent, string childName)
        {
            GameObject newChild = new GameObject(childName);
            newChild.transform.SetParent(parent);
            newChild.transform.localPosition = new Vector3(0, 0, 0);
            newChild.transform.localScale = new Vector3(1, 1, 1);
            RectTransform rectTransform = newChild.AddComponent<CanvasRenderer>().gameObject.AddComponent<RectTransform>();
            Image backGround = newChild.AddComponent<Image>();
            backGround.color = new Color(0, 0, 0, 0);
            rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
            rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
            rectTransform.offsetMin = rectTransform.offsetMax = new Vector2(0,0);
            rectTransform.sizeDelta = new Vector2( 10000, 10000);
            newChild.gameObject.SetActive(false);
            return (newChild.transform,backGround);
        }
    }

}

